<template>
    <div class="container">
        <div class="page-header">
            <div class="back-button">
                <el-button @click="$router.push('/main/biao')" icon="el-icon-back">返回</el-button>
            </div>
            <h1>背景调查表</h1>
            <div class="form-actions">
                <el-button v-if="!isView" type="primary" @click="saveData">保存</el-button>
                <el-button type="success" @click="goToList">列表</el-button>
                <el-button type="info" @click="handlePrint" icon="el-icon-printer">打印</el-button>
            </div>
        </div>

        <div class="main-content">

            <div class="print-content">
                <div class="print-header">
                    <h2>背景调查表</h2>
                </div>

                <el-form :model="formData" class="investigation-form">
                    <table border="1">
                        <!-- 基本信息 -->
                        <tr>
                            <td>姓 名</td>
                            <td><el-input v-model="formData.name" :disabled="isView" /></td>
                            <td>应聘职位</td>
                            <td><el-input v-model="formData.appliedPosition" :disabled="isView" /></td>
                        </tr>
                        <tr>
                            <td>原公司名称</td>
                            <td><el-input v-model="formData.previousCompany" :disabled="isView" /></td>
                            <td>在原公司的工作时间</td>
                            <td><el-input v-model="formData.workTimeInPreviousCompany" :disabled="isView" /></td>
                        </tr>
                        <tr>
                            <td>调查接待人</td>
                            <td><el-input v-model="formData.investigationReceiver" :disabled="isView" /></td>
                            <td>电 话</td>
                            <td><el-input v-model="formData.phoneNumber" :disabled="isView" /></td>
                        </tr>
                        <tr>
                            <td>职 位</td>
                            <td><el-input v-model="formData.position" :disabled="isView" /></td>
                            <td>薪金水平</td>
                            <td><el-input v-model="formData.salaryLevel" :disabled="isView" /></td>
                        </tr>
                        <tr>
                            <td>工作职责的简单描述</td>
                            <td colspan="3">
                                <el-input type="textarea" v-model="formData.jobResponsibilities" :disabled="isView"
                                    :rows="3" />
                            </td>
                        </tr>
                        <!-- 工作表现等信息 -->
                        <tr>
                            <td>工作表现</td>
                            <td colspan="3">
                                <el-input type="textarea" v-model="formData.workPerformance" :disabled="isView"
                                    :rows="3" />
                            </td>
                        </tr>
                        <tr>
                            <td>与同事、上司的关系</td>
                            <td colspan="3">
                                <el-input type="textarea" v-model="formData.relationship" :disabled="isView"
                                    :rows="3" />
                            </td>
                        </tr>
                        <tr>
                            <td>离职原因</td>
                            <td colspan="3">
                                <el-input type="textarea" v-model="formData.reasonForLeaving" :disabled="isView"
                                    :rows="3" />
                            </td>
                        </tr>
                        <tr>
                            <td>备 注</td>
                            <td colspan="3">
                                <el-input type="textarea" v-model="formData.remarks" :disabled="isView" :rows="3" />
                            </td>
                        </tr>
                        <!-- 调查人员签名和日期 -->
                        <tr>
                            <td>调查人员签名</td>
                            <td><el-input v-model="formData.investigatorSignature" :disabled="isView" /></td>
                            <td>调查日期</td>
                            <td>
                                <el-date-picker v-model="formData.investigationDate" type="date" :disabled="isView"
                                    value-format="yyyy-MM-dd" format="yyyy-MM-dd" placeholder="选择日期" />
                            </td>
                        </tr>
                    </table>
                </el-form>

                <div class="form-footer">
                    <p>表单编号及版本：AM - JY - 286 A/0</p>
                    <p>保存期限：长期</p>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
import backgroundInvestigationApi from '@/api/backgroundInvestigation';

export default {
    data() {
        return {
            formData: {
                id: null,
                name: '',
                appliedPosition: '',
                previousCompany: '',
                workTimeInPreviousCompany: '',
                investigationReceiver: '',
                phoneNumber: '',
                position: '',
                salaryLevel: '',
                jobResponsibilities: '',
                workPerformance: '',
                relationship: '',
                reasonForLeaving: '',
                remarks: '',
                investigatorSignature: '',
                investigationDate: new Date().toISOString().split('T')[0]
            },
            isView: false
        };
    },
    mounted() {
        const id = this.$route.query.id;
        const type = this.$route.query.type;

        if (id) {
            this.isView = type === 'view';
            this.fetchData(id);
        }
    },
    methods: {
        async fetchData(id) {
            try {
                const response = await backgroundInvestigationApi.getDetail(id);
                if (response.data) {
                    const data = response.data;
                    // 处理日期
                    if (data.investigationDate) {
                        data.investigationDate = data.investigationDate.split('T')[0];
                    }
                    this.formData = data;
                }
            } catch (error) {
                // console.error('获取数据失败', error);
                this.$message.error('获取数据失败：' + (error.response?.data || error.message));
            }
        },
        async saveData() {
            try {
                const response = await backgroundInvestigationApi.save(this.formData);
                if (response.data) {
                    this.$message.success('保存成功');
                    this.goToList();
                }
            } catch (error) {
                // console.error('保存失败', error);
                this.$message.error('保存失败：' + (error.response?.data || error.message));
            }
        },
        goToList() {
            this.$router.push('/main/biao/beidiao-list');
        },
        handlePrint() {
            window.print();
        }
    }
};
</script>

<style lang="scss" scoped>
.container {
    max-width: 1200px;
    margin: 0 auto;
    padding: 24px;
    background: linear-gradient(135deg, #f8f9fa 0%, #ffffff 100%);
    min-height: 100vh;
    position: relative;
}

.page-header {
    display: flex;
    align-items: center;
    justify-content: center;
    margin-bottom: 30px;
    padding: 20px;
    position: relative;
    background: rgba(255, 255, 255, 0.9);
    backdrop-filter: blur(10px);
    border-radius: 12px;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.05);

    h1 {
        font-size: 24px;
        color: #2c3e50;
        margin: 0;
        font-weight: 600;
        position: relative;
        padding-bottom: 10px;

        &::after {
            content: '';
            position: absolute;
            bottom: 0;
            left: 50%;
            transform: translateX(-50%);
            width: 60px;
            height: 3px;
            background: linear-gradient(90deg, #409EFF, #67C23A);
            border-radius: 3px;
        }
    }
}

.back-button {
    position: absolute;
    left: 20px;
    top: 50%;
    transform: translateY(-50%);

    .el-button {
        padding: 8px 15px;
        font-size: 14px;
        border-radius: 6px;
        transition: all 0.3s ease;

        &:hover {
            transform: translateX(-2px);
            box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
        }
    }
}

.main-content {
    background: rgba(255, 255, 255, 0.9);
    backdrop-filter: blur(10px);
    border-radius: 12px;
    padding: 24px;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.05);
}

.form-actions {
    position: absolute;
    right: 20px;
    top: 35%;
    margin-bottom: 20px;
    display: flex;
    gap: 12px;
    justify-content: flex-end;

    .el-button {
        padding: 10px 24px;
        font-size: 14px;
        border-radius: 6px;
        transition: all 0.3s ease;

        &:hover {
            transform: translateY(-2px);
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
        }
    }
}

.investigation-form {
    table {
        width: 100%;
        border-collapse: separate;
        border-spacing: 0;
        border-radius: 8px;
        overflow: hidden;
        border: 1px solid #ebeef5;
        margin-bottom: 24px;

        td {
            padding: 12px;
            border: 1px solid #ebeef5;
            vertical-align: middle;

            &:first-child {
                background-color: rgba(248, 249, 250, 0.5);
                font-weight: 500;
                color: #2c3e50;
                width: 160px;
            }

            .el-input,
            .el-date-picker {
                width: 100%;
            }

            .el-input__inner {
                height: 32px;
                line-height: 32px;
                padding: 0 12px;
            }

            .el-textarea__inner {
                padding: 8px 12px;
                min-height: 80px;
                resize: vertical;
            }
        }

        tr:hover {
            background-color: rgba(64, 158, 255, 0.05);
        }
    }
}

.form-footer {
    display: flex;
    justify-content: space-between;
    padding: 16px 0;
    color: #606266;
    font-size: 14px;
    border-top: 1px solid #ebeef5;
    margin-top: 20px;

    p {
        margin: 0;
    }
}

@media screen and (max-width: 768px) {
    .container {
        padding: 16px;
    }

    .page-header {
        padding: 16px;
        margin-bottom: 20px;

        h1 {
            font-size: 20px;
        }
    }

    .main-content {
        padding: 16px;
    }

    .form-actions {
        flex-direction: column;

        .el-button {
            width: 100%;
            margin: 4px 0;
        }
    }

    .investigation-form {
        table {
            td {
                padding: 8px;
                font-size: 1em;

                &:first-child {
                    width: 100px;
                }
            }
        }
    }

    .form-footer {
        flex-direction: column;
        text-align: center;
        gap: 8px;
    }
}

/* 打印样式优化 */
@media print {
    .container {
        margin: 0;
        padding: 0;
        background: none;
        min-height: auto;
    }

    .page-header,
    .form-actions,
    .back-button {
        display: none !important;
    }

    .main-content {
        padding: 0;
        background: none;
        box-shadow: none;
    }

    .print-header {
        display: block;
        text-align: center;
        margin-bottom: 20px;
        padding: 20px 0;
        border-bottom: 2px solid #000;

        h2 {
            font-size: 24px;
            font-weight: bold;
            margin: 0;
        }
    }

    .investigation-form {
        table {
            width: 100%;
            border-collapse: collapse;
            page-break-inside: auto;

            td {
                border: 1px solid #000;
                padding: 8px;
                font-size: 14px;

                &:first-child {
                    background: none;
                    font-weight: bold;
                    width: 160px;
                }
            }

            tr {
                page-break-inside: avoid;
                page-break-after: auto;
            }
        }

        .el-input__inner,
        .el-textarea__inner {
            border: none;
            padding: 0;
            font-size: 14px;
            color: #000;
            background: none;
        }

        .el-textarea__inner {
            min-height: 60px;
            line-height: 1.5;
        }
    }

    .form-footer {
        margin-top: 30px;
        padding-top: 20px;
        border-top: 1px solid #000;
        page-break-inside: avoid;

        p {
            margin: 5px 0;
            font-size: 12px;
            color: #000;
        }
    }

    /* 确保表格不会在页面中间断开 */
    table {
        page-break-inside: avoid;
    }

    /* 处理分页时的样式 */
    @page {
        margin: 2cm;
        size: A4 portrait;
    }

    /* 隐藏不需要打印的元素 */
    .el-button,
    .el-message,
    .el-message-box {
        display: none !important;
    }

    /* 优化文本域打印效果 */
    .el-textarea {
        .el-textarea__inner {
            resize: none;
            overflow: visible;
            height: auto !important;
        }
    }

    /* 确保长文本正确换行 */
    td {
        word-break: break-word;
        white-space: pre-wrap;
    }
    
  textarea {
    border: 0px !important;
  }
::v-deep input.el-input__inner {
    border: none !important;
  }
  ::v-deep textarea.el-textarea__inner {
    border: none !important;
    resize: none;
  }
  ::v-deep i.el-input__icon {
    display: none !important;
  }
}

::deep .el-input__inner,
.el-textarea__inner {
    border: none;
    padding: 0;
    font-size: 1em;
    color: #000;
    background: none;
}

/* 打印预览时隐藏的元素 */
@media screen {
    .print-header {
        display: none;
    }
}
</style>